package chapter07.filter;

import java.io.*;
import java.io.IOException;
import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;

public class MyFilter implements Filter {

    @Override
    public void destroy() {
        // 过滤器对象在销毁时自动调用，释放资源
    }

    @Override
    public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
            throws IOException, ServletException {
        // 用于拦截用户请求，如果和当前过滤器的拦截路径匹配，该方法会被调用
        PrintWriter out = response.getWriter();
        out.write("Hello MyFilter---------");
//        HttpServletResponse resp = (HttpServletResponse) response;
//        resp.getWriter().write("Hello MyFilter---------");
//        chain.doFilter(request, response);
    }

    @Override
    public void init(FilterConfig fConfig) throws ServletException {
        // 过滤器对象在初始化时调用，可以配置一些初始化参数
    }
}
